<?

require_once("MySmarty.php");
require_once("common.inc.php");





$o_smarty = new MySmarty();
$hash_key = hash('md5',$_GET['entry_id'].$_GET['f']);
$o_smarty->cache_lifetime = 60*60*24*30;//30日周期

//$o_smarty->cache_lifetime = 3600*1;//1時間周期
//echo "is_cache:".$o_smarty->is_cached('entry_embed2.tpl',$hash_key);
if(is_numeric($_GET['site_id'])){
	$site_id = $_GET['site_id'];
	$site_name_all = $site_name[$site_id];

}else{
	$site_name_all = $site_name[1];

}


if(is_numeric($_GET['width']) || is_numeric($_GET['height'])){

	$width = is_numeric($_GET['width']) ? $_GET['width'] : (int)$_GET['height']*1.5;
	$height = is_numeric($_GET['height']) ? $_GET['height'] : (int)$_GET['height']*2/3;

}else{

	$width = 600;
	$height = 480;
}
//echo $hash_key;

//if(!$o_smarty->is_cached('entry_embed.tpl',$hash_key)){
	

	$con = $o_smarty->dbconnect();
	$sql = "SET NAMES utf8";
	$o_smarty->doSQL($sql);
	
	

	if(isset($_GET['entry_id']) && is_numeric($_GET['entry_id'])){
		$entry_id = $_GET['entry_id'];
		
	}else{
		header("Location: index.php");
	}
	

	//共有サイト情報
	$site_arr = array();
	$sql = "SELECT * FROM site_mst";
	$result_site = doSQL($sql);
	while($row_site = mysql_fetch_array($result_site)){
	
		$site_id = $row_site['site_id'];
		$site_arr['site_name'][$site_id] = $row_site['site_name'];
		$site_arr['reg_url'][$site_id] = $row_site['reg_url'];
		$site_arr['reg_thumb'][$site_id] = $row_site['reg_thumb'];	
		$site_arr['reg_flashobj'][$site_id] = $row_site['reg_flashobj'];
				
	}

	
	$sql = "SELECT A.entry_id,A.title,A.entry_url,A.comment,A.description,A.up_date,A.urllist_id,B.blog_title,B.blog_url,A.site_id 
			FROM entrylist A LEFT JOIN urllist B on A.urllist_id = B.urllist_id 
			WHERE  A.entry_id=$entry_id";
			
//echo $sql;
	$result = $o_smarty->doSQL($sql);
	$row = mysql_fetch_array($result);
	
	$i = 0;
	
	
	
	$entry['title'] = $row['title'];
	$entry['entry_id'] = $row['entry_id'];
	$entry['entry_url'] = $row['entry_url'];
	$entry['comment'] = strip_tags($row['comment']);
	$entry['description'] = $row['description'];
	$entry['up_date'] = $row['up_date'];
	$entry['urllist_id'] = $row['urllist_id'];
	$entry['blog_title'] = $row['blog_title'] == "" ? "No Title" : $row['blog_title'];
	$entry['blog_url'] = $row['blog_url'];
	
	
	if($site_id==''){
		$entry['site_id'] = $row['site_id'];
		$site_id = $row['site_id'];
	}else{
		$entry['site_id'] = $site_id;
	
	}
	

	
	$sql = "SELECT * FROM videolist WHERE entry_id=".$row['entry_id'];
	$resultv = doSQL($sql);
	$entry['v_cnt'] = mysql_num_rows($resultv);
	
	
	$v = 0;
	while($rowv = mysql_fetch_array($resultv)){
		$cid = $rowv['cid'];
		$cid2 = $rowv['cid2'];
				
		$site_id = $rowv['site_id'];
		$flash_obj = $rowv['flash_obj'];		
		$url = $rowv['url'];
		$title = $rowv['title'];
		$v_comment = $rowv['v_comment'];		
		$view_cnt = $rowv['view_cnt'];		
		$up_date = $rowv['up_date'];

		if($site_id==''){
			$entry['site_id'] = $rowv['site_id'];
			$site_id = $rowv['site_id'];
		}

		if($entry['comment']==''){
			$entry['comment'] = $rowv['v_comment'];
		}

		$entry['time'] = $rowv['time'];
		$entry['time_disp'] = gmdate('H:i:s', $rowv['time']);	
		$entry['site_name'] = $site_arr['site_name'][$site_id];
				
		$entry['rating_avg'] = $rating_avg = $rowv['rating_avg'];		
		$entry['up_date_y'] = $up_date_y = $rowv['up_date_y'];
		$entry['view_cnt_y'] = $view_cnt_y = $rowv['view_cnt_y'];
		$entry['url'] = $url = $rowv['url'];
		
		
		$prefix = $prefix = substr($cid,0,2);

		if($cid!='' && $site_arr['reg_flashobj'][$site_id]!=''){					
			$flashobj_b = $site_arr['reg_flashobj'][$site_id];

			//$thumbnail_b = strtr($thumbnail_b, 'img', $cid);
			$flashobj_b = str_replace("{{cid}}", $cid, $flashobj_b);
			$flashobj_b = str_replace("{{cid2}}", $cid2, $flashobj_b);

			$flashobj_b = str_replace("{{width}}", $width, $flashobj_b);
			$flashobj_b = str_replace("{{height}}", $height, $flashobj_b);

			if($site_id==13){
				$prefix = substr($cid,0,2);
				$flashobj_b = str_replace("{{pre}}", $prefix, $flashobj_b);
			}
			
			$entry['flashObj'] = $flashobj_b;
			$thumbnail_b = "";
		}
		
		$v++;
	}
	
	//関連タグ
	$sql = "SELECT * FROM tag_list A LEFT JOIN tag_mst B ON  A.tag_id =B.tag_id WHERE A.entry_id=".$row['entry_id']." ORDER BY  B.entry_cnt DESC LIMIT 0,4";
	$result_tag = doSQL($sql);


	if(mysql_num_rows($result_tag)!=0){
		//パンくず(トップ階層)	
		$tree .= "<a href=\"index.php\">トップ</a> ";
		
		//パンくず(第二階層)
		$tree .= "> <a href=\"genrelist.php\">ジャンル一覧</a> ";
		$tree .= ">";

		$j=0;
		while($row_tag = mysql_fetch_array($result_tag)){
			if($j==0) $tag_disp = $row_tag['tag_name'];
			
			$tag[$j]['tag_name'] = $row_tag['tag_name'];
			$tag[$j]['tag_id'] = $row_tag['tag_id'];
		
			//パンくず(第三層)
			$tree .= " <a href=\"genre.php?tag_id=".$row_tag['tag_id']."\">[".$row_tag['tag_name']."] </a>";						
			
			$j++;
		}
	}


	
/*************************************************************************************/
/*カテゴリーツリー(ここから)
/*************************************************************************************/

	$CatListArray = fetchTree($lang_id);
	$keywordRstArray = fetchKeywordList($lang_id);
	
	
	$keyWordArray = getKeywordList($keywordRstArray);
	$html_str = "";
	$html_str = getCatList($CatListArray,1);
	//print_r($keyWordArray);
	

/*************************************************************************************/
/*カテゴリーツリー(ここまで)
/*************************************************************************************/
	
	


	







	//$o_smarty->dbclose($con);
	if( empty($entry) || empty($videos) || empty($filename) || empty($actress_ranking)){ 
		$o_smarty->caching = 0;
		$err_msg = "アクセス集中の為、正しく表示されない可能性があります。<br/>表示がおかしな場合は、お手数ですが時間をおいてから、再読み込みを行ってください。m(_ _)m";
	}
	
	//print_r($entry);

	$o_smarty->assign("err_msg",$err_msg);
	$o_smarty->assign("actress_ranking",$actress_ranking);
	$o_smarty->assign("genre_ranking",$genre_ranking);
	$o_smarty->assign("entry",$entry);
	//$o_smarty->assign("date",$date);
	$o_smarty->assign("tag",$tag);
	$o_smarty->assign("taga",$taga);
	$o_smarty->assign("videos",$videos);
	
	$o_smarty->assign("cid",$cid_v);
	$o_smarty->assign("tree",$tree);
	
	$o_smarty->assign("playing_f",$filename);
	$o_smarty->assign("tag_disp",$tag_disp);
	$o_smarty->assign("tag2_disp",$tag2_disp);
	$o_smarty->assign("site_name_all",$site_name_all);			
	$o_smarty->assign("tree_str",$html_str);
//}


$o_smarty->display("entry_embed.tpl",$hash_key);
//$o_smarty->display("entry_embed.tpl");
?>